package com.mirror.llk.dao;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.mirror.llk.bean.domain.GameBean;
import com.mirror.llk.bean.domain.TTheme;
import com.mirror.llk.utils.Constant;
import com.mirror.llk.utils.Sql;
import com.mirror.utils.base.Page;
import com.mirror.utils.db.DBHelper;

public class ThemeDao {

	private static final String TAG = "ThemeDao";

	private DBHelper dbHelper = new DBHelper();

	public ThemeDao() {
	}

	public Long saveLocal(TTheme theme) {
		Long localId = null;
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		database.execSQL(Sql.INSERT.THEME,
		                 new Object[] { theme.getId(), theme.getThemeName(), theme.getThemeDesc(), theme.getThemeType(), theme.getThemeDownload() });
		Cursor cursor = database.rawQuery(Sql.SELECT.THEME_MAX_LOCAL_ID, null);
		while (cursor.moveToNext()) {
			localId = cursor.getLong(0);
		}
		dbHelper.closeQuery(database, cursor);
		return localId;
	}

	public TTheme queryLocal(Long localId) {
		TTheme theme = null;
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery(Sql.SELECT.THEME_LOCAL, new String[] { String.valueOf(localId) });
		while (cursor.moveToNext()) {
			theme = new TTheme(cursor.getString(0), cursor.getString(1), cursor.getInt(3), cursor.getLong(4));
		}
		dbHelper.closeQuery(database, cursor);
		return theme;
	}

	public void deleteLocal(Long localId) {
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		database.execSQL(Sql.DELETE.LOCAL_THEME, new Object[] { localId });
		database.close();
	}

	public synchronized void batchMerge(List<TTheme> themeList) {
		List<Long> ids = queryIds();
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		try {
			database.beginTransaction();
			for (TTheme theme : themeList) {
				if (ids.contains(theme.getId())) {
					database.execSQL(Sql.UPDATE.THEME_DOWNLOAD, new Object[] { theme.getThemeDownload(), theme.getId() });
				} else {
					database.execSQL(Sql.INSERT.THEME, new Object[] { theme.getId(), theme.getThemeName(), theme.getThemeDesc(),
					        theme.getThemeType(), theme.getThemeDownload() });
				}
			}
			database.setTransactionSuccessful();
		}
		catch (SQLException e) {
			Log.e(TAG, Constant.SQL_EXCEPTION, e);
		}
		finally {
			database.endTransaction();
			database.close();
		}
	}

	public List<Long> queryIds() {
		List<Long> idList = new ArrayList<Long>();
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery(Sql.SELECT.THEME_IDS, null);
		while (cursor.moveToNext()) {
			idList.add(cursor.getLong(0));
		}
		dbHelper.closeQuery(database, cursor);
		return idList;

	}

	public List<TTheme> queryList(Page page) {
		if (page == null) {
			page = new Page(1, Integer.MAX_VALUE);
		}
		List<TTheme> themeList = new ArrayList<TTheme>();
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery(Sql.SELECT.THEME_LIST,
		                                  new String[] { String.valueOf(page.getPageSize()),
		                                          String.valueOf((page.getPageIndex() - 1) * page.getPageSize()) });
		while (cursor.moveToNext()) {
			themeList.add(new TTheme(cursor.getLong(0), cursor.getLong(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4), cursor
			        .getLong(5)));
		}
		dbHelper.closeQuery(database, cursor);
		return themeList;
	}

	/**
	 * 获取存档列表
	 * 
	 * @return
	 */
	public List<GameBean> queryHistoryList() {
		List<GameBean> historyList = new ArrayList<GameBean>();
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery(Sql.SELECT.THEME_HISTORY_LIST, new String[] {});
		while (cursor.moveToNext()) {
			historyList.add(new GameBean(Constant.HISTORY_TYPE, cursor.getInt(1), cursor.getInt(2), cursor.getInt(0), cursor.getInt(8), null, cursor
			        .getInt(4), cursor.getInt(3), cursor.getInt(6), cursor.getInt(7)));
		}
		dbHelper.closeQuery(database, cursor);
		return historyList;
	}

	/**
	 * 根据ID获取存档信息
	 * 
	 * @return
	 */
	public GameBean queryHistoryById(int historyId) {
		GameBean history = null;
		SQLiteDatabase database = dbHelper.getReadableDatabase();
		Cursor cursor = database.rawQuery(Sql.SELECT.THEME_HISTORY_LIST, new String[] { String.valueOf(historyId) });
		if (cursor.moveToNext()) {
			history = new GameBean(Constant.HISTORY_TYPE, cursor.getInt(1), cursor.getInt(2), historyId, cursor.getInt(8), null, cursor.getInt(4),
			        cursor.getInt(3), cursor.getInt(6), cursor.getInt(7));
		}
		dbHelper.closeQuery(database, cursor);
		return history;
	}
}
